import { assign } from 'lodash'
import { fetchSaveData, fetchGetPipNetworkData } from '@/api/modeling'

const defaultState = () => {
  return {
    defaultDate: undefined,
    imgUrl: '',
    hasMd: false,
    hasLx: false,
    toolType: undefined,
    mdList: [],
    lxList: [],
    rangePoint: [],
    showRangePoint: true,
    okRangePoint: false,
    hasLxEdit: false,
    windowW: 0,
    windowH: 0,
    hasEdited: false,
    hasShowEditBtn: false
  }
}

export default {
  namespaced: true,
  name: 'modeling',
  state: defaultState(),
  getters: {
    //
  },
  mutations: {
    REST_STATE(state) {
      state = assign(state, defaultState())
    },
    SET_IMG_URL(state, url) {
      state.imgUrl = url
    },
    SET_HAS_MD(state, flag) {
      state.hasMd = flag
    },
    SET_HAS_LX(state, flag) {
      state.hasLx = flag
    },
    SET_TOOL_TYPE(state, type) {
      state.toolType = type
    },
    SET_MD_LIST(state, list) {
      state.mdList = list
    },
    SET_LX_LIST(state, list) {
      state.lxList = list
    },
    SET_RANGE_POINT(state, list) {
      state.rangePoint = list
    },
    SET_SHOW_RANGE_POINT(state, flag) {
      state.showRangePoint = flag
    },
    SET_OK_RANGE_POINT(state, flag) {
      state.okRangePoint = flag
    },
    SET_HAS_LX_EDIT(state, flag) {
      state.hasLxEdit = flag
    },
    SET_WINDOW_W(state, w) {
      state.windowW = w
    },
    SET_WINDOW_H(state, h) {
      state.windowH = h
    },
    SET_HAS_EDITED(state, flag) {
      state.hasEdited = flag
    },
    SET_HAS_EDIT_BTN(state, flag) {
      state.hasShowEditBtn = flag
    }
  },
  actions: {
    clearStore(ctx) {
      ctx.commit('REST_STATE')
    },
    async uploadImg({ commit }, file) {
      const reader = new FileReader()
      reader.readAsDataURL(file)
      reader.onload = e => {
        const code = e.target.result
        commit('SET_IMG_URL', code)
      }
      commit('SET_HAS_MD', false)
      commit('SET_HAS_LX', false)
      commit('SET_TOOL_TYPE', undefined)
      commit('SET_MD_LIST', [])
      commit('SET_LX_LIST', [])
      commit('SET_RANGE_POINT', [])
      commit('SET_SHOW_RANGE_POINT', true)
      commit('SET_OK_RANGE_POINT', false)
      commit('SET_HAS_LX_EDIT', false)
    },
    changeImgUrl({ commit }, url) {
      commit('SET_IMG_URL', url)
    },
    changeHasMd({ commit }, flag) {
      commit('SET_HAS_MD', flag)
    },
    changeHasLx({ commit }, flag) {
      commit('SET_HAS_LX', flag)
    },
    changeToolType({ commit }, type) {
      commit('SET_TOOL_TYPE', type)
    },
    changeMdList({ commit }, list) {
      commit('SET_MD_LIST', list)
    },
    changeLxList({ commit }, list) {
      commit('SET_LX_LIST', list)
    },
    changeRangePoint({ commit }, list) {
      commit('SET_RANGE_POINT', list)
    },
    changeShowRangePoint({ commit }, flag) {
      commit('SET_SHOW_RANGE_POINT', flag)
    },
    changeOkRangePoint({ commit }, flag) {
      commit('SET_OK_RANGE_POINT', flag)
    },
    changeHasLxEdit({ commit }, flag) {
      commit('SET_HAS_LX_EDIT', flag)
    },
    changeWindowW({ commit }, w) {
      commit('SET_WINDOW_W', w)
    },
    changeWindowH({ commit }, h) {
      commit('SET_WINDOW_H', h)
    },
    async loadSaveData({ commit }, data) {
      const ret = await fetchSaveData(data)
      return ret
    },
    async loadParams({ commit }, data) {
      const ret = await fetchGetPipNetworkData()
      return ret
    },
    changeHasEdited({ commit }, flag) {
      commit('SET_HAS_EDITED', flag)
    },
    changeHasEditBtn({ commit }, flag) {
      commit('SET_HAS_EDIT_BTN', flag)
    }
  }
}
